/**
 * Created with IntelliJ IDEA.
 * User: aermakov
 * Date: 23.05.13
 * Time: 15:19
 * To change this template use File | Settings | File Templates.
 */

Ext.ns('GrailsCompany.data');

GrailsCompany.data.WritableJsonStore = Ext.extend(Ext.data.JsonStore, {
    constructor: function (config) {
        config = Ext.apply({
            autoSave: true,
            autoLoad: true,
            root: 'data'
        }, config);

        GrailsCompany.data.WritableJsonStore.superclass.constructor.call(this, config);

        if (this.proxy) {
            this.proxy.on('beforewrite', function (proxy, action, rs, params) {
                if (action === 'destroy') {
                    params.data = Ext.encode({
                        id: params.data
                    })
                }
            }, this);
        }

        this.addEvents({
            'create': true
        });
    },

    writer: new Ext.data.JsonWriter({
        writeAllFields: true
    }),

    onCreateRecords: function (success, rs, data) {
        GrailsCompany.data.WritableJsonStore.superclass.onCreateRecords.call(this, success, rs, data);
        this.fireEvent('create', this, rs);
    }
});